<?php
$title = '策略管理';
/** @var array $scripts */
/** @var array|null $created */
/** @var string|null $flash */
require __DIR__ . '/../partials/head.php';
require __DIR__ . '/../partials/nav.php';
?>
<section class="section">
    <div class="app-container" style="display:flex;flex-direction:column;gap:24px;">
        <?php if ($flash): ?>
            <div class="card" style="background:rgba(10,132,255,0.08);border:1px solid rgba(10,132,255,0.2);color:#0a84ff;">
                <?= htmlspecialchars($flash) ?>
            </div>
        <?php endif; ?>
        <div class="card" style="gap:20px;">
            <h2 style="margin:0;">新建策略</h2>
            <form method="post" action="/admin/strategies/create" style="display:grid;gap:16px;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));">
                <div class="field">
                    <label>策略代码</label>
                    <input type="text" name="code" required>
                </div>
                <div class="field">
                    <label>名称</label>
                    <input type="text" name="name" required>
                </div>
                <div class="field">
                    <label>Connector</label>
                    <input type="text" name="connector" value="gate_io_perpetual" required>
                </div>
                <div class="field">
                    <label>交易对（逗号分隔）</label>
                    <input type="text" name="symbols" value="ETH-USDT" required>
                </div>
                <div class="field">
                    <label>默认额度（USDT）</label>
                    <input type="number" name="default_value" value="100" min="10" required>
                </div>
                <div class="field">
                    <label>脚本限流/每分钟</label>
                    <input type="number" name="rate" value="30" min="1">
                </div>
                <div style="grid-column:1 / -1;display:flex;justify-content:flex-end;">
                    <button class="btn" type="submit">创建策略</button>
                </div>
            </form>
            <?php if ($created): ?>
                <div class="card" style="background:rgba(255,255,255,0.6);">
                    <h3 style="margin-top:0;">已创建：<?= htmlspecialchars($created['name']) ?></h3>
                    <p>Webhook URL：</p>
                    <code id="created-url" style="word-break:break-all;display:block;padding:8px 12px;background:rgba(0,0,0,0.05);border-radius:12px;"><?= htmlspecialchars($created['webhook_url']) ?></code>
                    <button class="btn-ghost btn" data-copy="#created-url" type="button">复制 URL</button>
                    <p>Body Secret：</p>
                    <code id="created-secret" style="word-break:break-all;display:block;padding:8px 12px;background:rgba(0,0,0,0.05);border-radius:12px;"><?= htmlspecialchars($created['body_secret']) ?></code>
                    <button class="btn-ghost btn" data-copy="#created-secret" type="button">复制 Secret</button>
                    <p>JSON 模板：</p>
                    <pre id="created-json" style="background:rgba(0,0,0,0.05);padding:12px;border-radius:12px;overflow:auto;"><?= htmlspecialchars(json_encode($created['payload_template'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)) ?></pre>
                    <button class="btn-ghost btn" data-copy="#created-json" type="button">复制 JSON</button>
                    <p>Pine v5 片段：</p>
                    <pre id="created-pine" style="background:rgba(0,0,0,0.05);padding:12px;border-radius:12px;overflow:auto;white-space:pre-wrap;"><?= htmlspecialchars($created['pine_snippet']) ?></pre>
                    <button class="btn-ghost btn" data-copy="#created-pine" type="button">复制 Pine</button>
                </div>
            <?php endif; ?>
        </div>
        <div class="card" style="gap:18px;">
            <h3 style="margin:0;">已上线策略</h3>
            <table class="table">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>代码</th>
                    <th>Connector</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($scripts as $script): ?>
                    <tr>
                        <td><?= htmlspecialchars((string)$script['id']) ?></td>
                        <td><?= htmlspecialchars($script['code']) ?></td>
                        <td><?= htmlspecialchars($script['connector']) ?></td>
                        <td><?= htmlspecialchars($script['status']) ?></td>
                        <td style="display:flex;gap:10px;">
                            <form method="post" action="/admin/strategies/<?= $script['id'] ?>/pause">
                                <button class="btn-ghost btn" type="submit" data-confirm="确认暂停该策略？">暂停</button>
                            </form>
                            <form method="post" action="/admin/strategies/<?= $script['id'] ?>/resume">
                                <button class="btn-ghost btn" type="submit">恢复</button>
                            </form>
                            <form method="post" action="/admin/strategies/<?= $script['id'] ?>/rotate">
                                <button class="btn-ghost btn" type="submit">旋转密钥</button>
                            </form>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>
<?php require __DIR__ . '/../partials/footer.php'; ?>
